local ClientOHOH = _G.ClientOHOH;
local TweenService = game:GetService("TweenService");
local M = ClientOHOH.class.new(script,ClientOHOH.files.OHOHOLYBUIBase())
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
function InitInsts(ui)
    ui.insts = {};
    ui.insts.root = "ctrl.root";
    ui.insts.cell = "ctrl.cell"
    ui.insts.frame = "ctrl.frame"
    
end

local cellStr = {
    icon = "ctrl.icon",
    percentText = "ctrl.weight",
    nameText = "ctrl.name",
    cell = "ctrl.cell",
    changeSize = "ctrl.changeSize"
}

function M.new(...)
    return M(...);
end

function M.ctor(self)
    InitInsts(self);
    M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.WeaponDropEffectGui"));
    self.insts.cell.Visible = false
end

function M.OnShow(self,iconUrl,percentText,name)
    local effectObj = self.insts.cell:Clone()
    local effect = self:InitUiInst2(effectObj,cellStr)
    effect.cell.Parent = self.insts.root
    effect.cell.Visible = true
    self.main_inst.Enabled = true
    effect.percentText.Text = percentText
    effect.nameText.Text = name
    effect.icon.Image = iconUrl
    self.Animation(self,effect,effectObj)
end

function M.Animation(self,effect,effectObj)

    self:AddProcess(function()
        self:Wait(1.5)
        local targetUDim2 = self.GetPlayerUdim2Coordinate();
        local tweenInfo = TweenInfo.new(
            0.6,
            Enum.EasingStyle.Quart,
            Enum.EasingDirection.Out,
            0,
            false,
            0
        )
        local goal = {
            Position = targetUDim2,
            Size = UDim2.new(0.03,0,0.11,0)
        }

        local tween = TweenService:Create(effect.cell,tweenInfo,goal)

        tween.Completed:Connect(function()
            effectObj:Destroy()
        end)
        effect.cell.Parent = self.insts.frame
        tween:Play();
    end)
    
end

function M.GetPlayerUdim2Coordinate()
    local HumanoidRootPart = player.Character:WaitForChild("HumanoidRootPart");
    local playerWorldPosition;
    if HumanoidRootPart then
        playerWorldPosition = HumanoidRootPart.CFrame.Position;

    end
    local targetPosition = camera:WorldToScreenPoint(playerWorldPosition)
    --手动修改y轴添加指减小因为相机角度不同获得屏幕坐标的误差
    return UDim2.new(0,targetPosition.X,0,targetPosition.Y+43)
end



function M.dtor(self)
    M.super.dtor(self);
end

return M;